home *** CD-ROM | disk | FTP | other *** search
/ BCI NET 2 / BCI NET 2.iso / archives / programming / gui / precog2_1.lha / Precognition2_1 / src / src.lha / Precognition / BuilderWindow.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-12-18  |  10.9 KB  |  426 lines

  1. #include "BuilderWindow.h"
  2. #include "BuilderMethods/BuilderMethods.h"
  3. #include "Menus/MenuStuff.h"
  4. #include "AmigaMem.h"
  5. #include "project.h"
  6. #ifndef __GNUC__
  7. #include <clib/exec_protos.h>
  8. #include <clib/graphics_protos.h>
  9. #include <clib/intuition_protos.h>
  10. #endif
  11. #ifdef __GNUC__
  12. #include <proto/exec.h>
  13. #include <proto/graphics.h>
  14. #include <proto/intuition.h>
  15. #endif
  16. #ifdef __SASC
  17. #include <proto/exec.h>
  18. #include <proto/graphics.h>
  19. #include <proto/intuition.h>
  20. #endif
  21.  
  22. #define SELF &self->pw
  23.  
  24. void
  25. BuilderWindow_AddPObjectButDontRender( BuilderWindow  *self,
  26.                                        GraphicObject  *obj )
  27. {
  28.    Point location;
  29.    /* EDB */
  30.    /* Additions for snapping objects inside window borders */
  31.    Point window_size, object_size;
  32.    Window *iwindow;
  33.    short leftborder, rightborder, topborder, bottomborder;
  34.  
  35.    iwindow = iWindow( &self->pw );
  36.    leftborder = iwindow->BorderLeft;
  37.    rightborder = iwindow->BorderRight;
  38.    topborder = iwindow->BorderTop;
  39.    bottomborder = iwindow->BorderBottom;
  40.  
  41.    window_size = Size( (GraphicObject *)&self->pw );
  42.  
  43.    location = Location( obj );
  44.    object_size = Size( obj );
  45.       if( ( location.x  > leftborder ) &&
  46.           ( ( location.x + object_size.x) < (window_size.x - rightborder) ) )
  47.         {
  48.          /* if object is within window borders */
  49.         if (self->eb.gridinfo)
  50.             location = GridSnap( location, self->eb.gridinfo->location );
  51.         }
  52.  
  53.       else /* snap to inside left or right window border */
  54.         {
  55.         if( location.x > leftborder )
  56.            /* right of left border, snap to right border */
  57.            location.x = (window_size.x - rightborder) - object_size.x;
  58.         else /* snap to left border */
  59.            location.x = leftborder;
  60.         }
  61.  
  62.       if( ( location.y > topborder ) &&
  63.           ( (location.y + object_size.y) < (window_size.y - bottomborder) ) )
  64.         {
  65.          /* if object is within window borders */
  66.         if (self->eb.gridinfo)
  67.             location = GridSnap( location, self->eb.gridinfo->location );
  68.         }
  69.       else /* snap to inside top or bottom window border */
  70.         {
  71.         if( location.y > topborder )
  72.            /* below top border, snap to bottom */
  73.            location.y = window_size.y - bottomborder - object_size.y;
  74.         else /* snap to top border */
  75.            location.y = topborder;
  76.         }
  77.  
  78.    SetLocation( obj, location.x, location.y );
  79.    AddWindowPObject( (struct pcgWindow *)self, obj );
  80. }
  81.  
  82. void
  83. BuilderWindow_AddPObject( BuilderWindow  *self,
  84.                           GraphicObject  *obj )
  85. {
  86.    BuilderWindow_AddPObjectButDontRender( self, obj );
  87.    Render( (struct GraphicObject *)SELF, NULL );
  88. }
  89.  
  90. void BuilderWindow_CleanUp( BuilderWindow *self )
  91. {
  92.    GraphicObject *obj;
  93.  
  94.    while( ( obj = (struct GraphicObject *)self->pw.FirstInteractor ) != NULL )
  95.    {
  96.       RemoveWindowPObject( (struct pcgWindow *)self, obj );
  97.       if( obj != (GraphicObject*) &self->eb )
  98.       {
  99.          Free( (struct PObject *)obj );
  100.       }
  101.       else
  102.       {
  103.          CleanUp( (struct PObject *)obj );
  104.       }
  105.    }
  106.  
  107.    while( ( obj = self->pw.FirstGraphic ) != NULL )
  108.    {
  109.       RemoveWindowPObject( (struct pcgWindow *)self, obj );
  110.       Free( (struct PObject *)obj );
  111.    }
  112. }
  113.  
  114. void BuilderWindow_PObjectToFront( BuilderWindow *self, GraphicObject *obj )
  115. {
  116.    GraphicObject *gfxobj, *first_obj;
  117.    GraphicObject **start_of_chain;
  118.  
  119.    if( isa( (struct PObject *)obj, InteractorClass() ) )
  120.    {
  121.       start_of_chain = (GraphicObject **)&self->eb.Next;
  122.       /* The EditBox is always the FIRST interactor */
  123.    }
  124.    else /* must be a graphic. */
  125.    {
  126.       start_of_chain = &self->pw.FirstGraphic;
  127.    }
  128.  
  129.    first_obj = *start_of_chain;
  130.    if( first_obj != obj ) /* already at start. */
  131.    {
  132.       for( gfxobj = first_obj; gfxobj != NULL; gfxobj = gfxobj->Next )
  133.       {
  134.          if( gfxobj->Next == obj )
  135.          {
  136.             gfxobj->Next    = obj->Next;
  137.             *start_of_chain = obj;
  138.             obj->Next       = first_obj;
  139.             break;
  140.          }
  141.       }
  142.    }
  143.  
  144.    Project_Modify();
  145.  
  146. }
  147.  
  148. void BuilderWindow_PObjectToBack( BuilderWindow *self, GraphicObject *obj )
  149. {
  150.    GraphicObject *gfxobj;
  151.    GraphicObject **start_of_chain;
  152.  
  153.    if( isa( (struct PObject *)obj, InteractorClass() ) )
  154.    {
  155.       start_of_chain = (GraphicObject **)&self->eb.Next;
  156.    }
  157.    else /* must be a graphic. */
  158.    {
  159.       start_of_chain = &self->pw.FirstGraphic;
  160.    }
  161.  
  162.  
  163.    /* Remove object. */
  164.    if( *start_of_chain == obj ) /* already at start. */
  165.    {
  166.       *start_of_chain = obj->Next;
  167.       obj->Next       = NULL;
  168.    }
  169.    else
  170.    {
  171.       for( gfxobj = *start_of_chain; gfxobj != NULL; gfxobj = gfxobj->Next )
  172.       {
  173.          if( gfxobj->Next == obj )
  174.          {
  175.             gfxobj->Next = obj->Next;
  176.             obj->Next    = NULL;
  177.             break;
  178.          }
  179.       }
  180.    }
  181.  
  182.    /* Now that object has been removed, add to tail of chain. */
  183.    if( *start_of_chain == NULL )
  184.    {
  185.       *start_of_chain = obj;
  186.    }
  187.    else
  188.    {
  189.       for ( gfxobj = *start_of_chain;
  190.             gfxobj->Next != NULL;
  191.             gfxobj = gfxobj->Next );
  192.  
  193.       gfxobj->Next = obj;
  194.    }
  195.  
  196.    Project_Modify();
  197.  
  198. }
  199.  
  200.  
  201. BOOL BuilderWindow_TestForHit( GraphicObject *self, Point coord )
  202. {
  203.    Point size, location;
  204.  
  205.    location = Location( self );
  206.    size     = Size( self );
  207.  
  208.    return( (BOOL)( ( coord.x >= location.x ) && ( coord.x < ( location.x+size.x ) ) &&
  209.            ( coord.y >= location.y ) && ( coord.y < ( location.y+size.y ) ) ) );
  210. }
  211.  
  212. GraphicObject *BuilderWindow_FindPObject( BuilderWindow *self,
  213.                                          Point          coord )
  214. {
  215.    GraphicObject *obj;
  216.  
  217.    /* Search Interactors first. */
  218.    for( obj = self->eb.Next; obj != NULL; obj = obj->Next )
  219.    {
  220.       if( BuilderWindow_TestForHit( obj, coord ) )
  221.          return obj;
  222.    }
  223.  
  224.    for( obj = self->pw.FirstGraphic; obj != NULL; obj = obj->Next )
  225.    {
  226.       if( BuilderWindow_TestForHit( obj, coord ) )
  227.          return obj;
  228.    }
  229.  
  230.    return NULL;
  231. }
  232.  
  233.  
  234. void BuilderWindow_AddInteractor( BuilderWindow  *self,
  235.                                   Interactor     *interactor )
  236. {
  237.    Interactor  *iactor;
  238.    ULONG       flags;
  239.  
  240.    SetInteractorWindow( interactor, SELF );
  241.  
  242.  
  243.    /* add this interactor to chain of interactors. */
  244.    if( self->pw.FirstInteractor )
  245.    {
  246.       for( iactor = self->pw.FirstInteractor;
  247.            iactor->Next != NULL;
  248.            iactor = iactor->Next );
  249.  
  250.       iactor->Next = interactor;
  251.    }
  252.    else
  253.       self->pw.FirstInteractor = interactor;
  254.  
  255.    /* Add IDCMP flags */
  256.  
  257.    flags = IDCMPFlags( (struct Interactor *)SELF ) | IDCMPFlags( interactor );
  258.    SetIDCMPFlags( SELF, flags );
  259. }
  260.  
  261.  
  262. void BuilderWindow_RemoveInteractor( BuilderWindow  *self,
  263.                                      Interactor     *interactor )
  264. {
  265.    Interactor    *iactor;
  266.  
  267.    SetInteractorWindow( interactor, NULL );
  268.  
  269.    if( self->pw.FirstInteractor )
  270.    {
  271.       if( self->pw.FirstInteractor == interactor )
  272.       {
  273.          self->pw.FirstInteractor = interactor->Next;
  274.       }
  275.       else
  276.       {
  277.          for(  iactor  = self->pw.FirstInteractor;
  278.                iactor != NULL;
  279.                iactor  = iactor->Next )
  280.          {
  281.             if( iactor->Next == interactor )
  282.             {
  283.                iactor->Next = interactor->Next;
  284.                break;
  285.             }
  286.          }
  287.       }
  288.    }
  289.    interactor->Next = NULL;
  290.  
  291.    Project_Modify();
  292. }
  293.  
  294. UWORD BuilderWindow_Respond( BuilderWindow *self, IntuiMessage *event )
  295. {
  296.    UWORD response = 0;
  297.    Window *w;
  298.  
  299.  
  300.    w = event->IDCMPWindow;
  301.  
  302.    switch( event->Class )
  303.    {
  304.       case REFRESHWINDOW:
  305.          if( w == self->pw.Window )
  306.          {
  307.             BeginRefresh( self->pw.Window );
  308.             Refresh( (struct Interactor *)SELF );
  309.             EndRefresh( self->pw.Window, TRUE );
  310.          }
  311.          response = RESPONDED;
  312.          break;
  313.  
  314.       case NEWSIZE:
  315.          response = RESPONDED;
  316.          Project_Modify();
  317.          break;
  318.  
  319.       default:
  320.          response = Respond( (struct Interactor *)&self->eb, event );
  321.  
  322.    }
  323.    return response;
  324. }
  325.  
  326.  
  327. void BuilderWindow_Refresh( BuilderWindow *self )
  328. {
  329.    GraphicObject     *graphic;
  330.    Interactor        *interactor;
  331.    RastPort          *rport;
  332.    struct Window     *iwindow;
  333.  
  334.    if( iwindow = iWindow( SELF ) )
  335.    {
  336.       RefreshWindowFrame( iwindow );
  337.  
  338.       rport = RPort( SELF );
  339.  
  340.       /* Draw window graphics */
  341.       for( graphic = self->pw.FirstGraphic;
  342.            graphic != NULL;
  343.            graphic = graphic->Next )
  344.       {
  345.          Render( graphic, rport );
  346.       }
  347.  
  348.       /* Draw interactors *** Draw EditBox LAST! */
  349.       for ( interactor  = self->eb.Next;
  350.             interactor != NULL;
  351.             interactor  = interactor->Next )
  352.       {
  353.          Refresh( interactor );
  354.       }
  355.  
  356.       Refresh( (struct Interactor *)&self->eb );
  357.    }
  358. }
  359.  
  360.  
  361. #include "BuilderMethods/BuilderWindow_builder.h"
  362.  
  363. struct BuilderMethods BuilderWindow_bm;
  364.  
  365. BOOL BuilderWindow_elaborated = FALSE;
  366.  
  367. struct pcgWindowClass BuilderWindow_Class;
  368.  
  369. void BuilderWindowClass_Init( struct pcgWindowClass *class )
  370. {
  371.    pcgWindowClass_Init( class );
  372.    class->isa                 = pcgWindowClass();
  373.    class->ClassName           = "my_window";
  374.    class->CleanUp             = (void(*)(PObject *))BuilderWindow_CleanUp;
  375.    class->AddInteractor       = (void(*)(struct pcgWindow *, struct Interactor *))BuilderWindow_AddInteractor;
  376.    class->RemoveInteractor    = (void(*)(struct pcgWindow *, struct Interactor *))BuilderWindow_RemoveInteractor;
  377.    class->BuilderMethods      = &BuilderWindow_bm;
  378.    class->Refresh             = (void(*)(Interactor *))BuilderWindow_Refresh;
  379.    class->Respond             = (USHORT(*)(Interactor *, IntuiMessage *))BuilderWindow_Respond;
  380.  
  381.    BuilderWindow_bm.PropEdit  = (void(*)(struct GraphicObject *,struct pcgWindow *,pcg_3DPens))BuilderWindow_PropEdit;
  382.    BuilderWindow_bm.WriteCode = (void(*)(struct GraphicObject *,struct BuilderWindow *,FILE *,UWORD))BuilderWindow_WriteCode;
  383.  
  384. }
  385.  
  386.  
  387. struct pcgWindowClass *BuilderWindowClass( void )
  388. {
  389.    if( ! BuilderWindow_elaborated )
  390.    {
  391.       BuilderWindowClass_Init( &BuilderWindow_Class );
  392.       BuilderWindow_elaborated = TRUE;
  393.    }
  394.  
  395.    return &BuilderWindow_Class;
  396. }
  397.  
  398.  
  399.  
  400. void BuilderWindow_Init( BuilderWindow *self,
  401.                          UWORD          leftedge,
  402.                          UWORD          topedge,
  403.                          UWORD          width,
  404.                          UWORD          height,
  405.                          pcg_3DPens     pens,
  406.                          char          *title,
  407.                          struct Screen *screen,
  408.                          GridInfo      *gridinfo )
  409. {
  410.  
  411.    pcgWindow_Init( SELF, leftedge, topedge, width, height,
  412.       100, 50, 65535, 65535, title,
  413.       CLOSEWINDOW | REFRESHWINDOW | MENUPICK | MENUVERIFY | NEWSIZE,
  414.       ACTIVATE | SIMPLE_REFRESH | WINDOWCLOSE | WINDOWDEPTH | WINDOWDRAG
  415.          | WINDOWSIZING | SIZEBBOTTOM ,
  416.       screen );
  417.  
  418.    self->pw.isa = BuilderWindowClass();
  419.    self->IDCMPFlags = REFRESHWINDOW | CLOSEWINDOW;
  420.    Editor_Init( &self->eb, pens, gridinfo );
  421.    AddWindowPObject( (struct pcgWindow *)SELF, (struct GraphicObject *)&self->eb );
  422.  
  423.    GiveItAName( (struct PObject *)self );
  424. }
  425.  
  426.